// pages/test2/test2.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
         isHide:true,
        canIUse: wx.canIUse('button.open-type.getUserInfo'),
         userData:null,
          image_Device_online: "../Image/Device_online.png",
          image_Device_offline: "../Image/Device_offline.png",
          Device_status: "../Image/Device_offline.png",
          // isShowConfirm: false,
          UUID: "请输入设备UUID码",
          // deviceid:"",
          // existDevice:false,
          // existDevice_id:"",

          AuthorizationValue: "",
          userId: "",
          deviceId: "",
          deviceKey: "",
          productId: "",
          productKey: "",
          modalName: null, //
          count: 10,  //

           top: '设置服药时间',
    bottom: '开启服药提醒',
    MTime: '设置上午服药时间',
    ATime: '设置下午服药时间',
    NTime: '设置晚上服药时间',
    mt: "",
    at: "",
    nt: "",
    imgList: "http://qt6d5m83p.hn-bkt.clouddn.com/yao.png",
    show1: '',   //胶囊显示窗口
    show2: '',   //服药次数显示窗口
    show3: '',   //药片显示窗口
    countTitle: '', //弹出框的标题
    //  DayCountTitle:'每次服用胶囊的数量?',
    // YaoCountTitle:'总服药次数?',
    // DayCountValue:'',
    //  YaoCountValue:'',
    pillValue: '', //药片数量值
    capsuleValue: '', //胶囊数量值
    MedicineCountValue: '',// 服用次数值
    DayAndYaoTitle: '设置药品数量',
    DrugsQuantityTitle: '设置药品数量',
    TimesOfTakingMedicine: "设置服药次数"
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var username = wx.getStorageSync('username')
    var that = this
    wx.request({
      url: "http://ypbitx.natappfree.cc/MyServlet/findUserInfo?username=" + username,
      method: 'GET',
      header: {
        'content-type': 'application/json',
      },
      success(res) {
        // console.log("获取用户信息请求成功", res)
        that.setInitData(res.data)
      },
      fail(res) {
        console.log("获取用户信息请求失败", res)
      }
    })
  },

  Time() {     //发送定时的时间
    var mt = this.data.mt;
    //mt="0/1:*"
    var at = this.data.at;
    var nt = this.data.nt;
    var capsuleValue = this.data.capsuleValue;
    var pillValue = this.data.pillValue;
    var yc = this.data.MedicineCountValue;
    var username = wx.getStorageSync('username')
    
    wx.request({    //设置定时任务
      url: "http://ypbitx.natappfree.cc/MyServlet/timer?" +
        "Mtimer=" + mt + "&Atimer=" + at + "&Ntimer=" + nt +
        "&capsuleValue=" + capsuleValue + "&pillValue=" + pillValue +
        "&YaoCont=" + yc + "&username=" + username,
      method: 'GET',
      header: {
        'content-type': 'application/json',
      },
      success(res) {
        // console.log("定时任务请求成功", res)

        wx.requestSubscribeMessage({      //订阅消息
          tmplIds: ['F9-8i2cMzJZ0jB_GJfAvN9oV_tZBomvjfljTr7K-Bc8',
            '6ed2dBY_OOF7Hbo-mtgYpYK4rmwff1H5j1GMJx6RCGo'],
          success(res) {
            console.log("订阅消息成功", res)
          }
        })

        wx.showToast({
          title: '操作成功！', // 标题
          icon: 'success',  // 图标类型，默认success
          duration: 1500  // 提示窗停留时间，默认1500ms
        })
      },
      fail(res) {
        console.log("定时任务请求失败", res)
      }
    })

    wx.request({    //存储到数据库
      url: "http://ypbitx.natappfree.cc/MyServlet/userTimerInsert?" +
        "Mtimer=" + mt + "&Atimer=" + at + "&Ntimer=" + nt +
        "&capsuleValue=" + capsuleValue + "&pillValue=" + pillValue +
        "&YaoCont=" + yc + "&username=" + username,
      method: 'GET',
      header: {
        'content-type': 'application/json',
      },
      success(res) {
        // console.log("存储数据库请求成功", res)
      },
      fail(res) {
        console.log("存储数据库请求失败", res)
      }
    })
  },
  showModal() {
    this.setData({
      show1: "show",
      countTitle: "每次服用胶囊的数量?"
    })
  },
  showModa3() {
    this.setData({
      show3: "show",
      countTitle: "服用药品的次数?"
    })
  },
  hideModal1() {
    this.setData({
      show1: "",
      countTitle: "每次服用药片的数量?",
      show2: "show"
    })
  },
  hideModal2() {
    this.setData({
      show2: "",
      DrugsQuantityTitle: '每次共服用胶囊' + this.data.capsuleValue + '粒,药片' + this.data.pillValue + '粒'
    })
  },
  hideModal3() {
    this.setData({
      show3: "",
      TimesOfTakingMedicine: '共服用' + this.data.MedicineCountValue + '次药品'
    })
  },
  setMT: function (e) {
    this.setData({
      mt: e.detail.value,
      MTime: e.detail.value,
      Mtitle: '上午服药时间:'
    })
  },
  setAT: function (e) {
    this.setData({
      at: e.detail.value,
      ATime: e.detail.value,
      Atitle: '中午服药时间:'
    })
  },
  setNT: function (e) {
    this.setData({
      nt: e.detail.value,
      NTime: e.detail.value,
      Ntitle: '晚上服药时间:'
    })
  },
  setInitData(e) {
    if (e != "") {
      var at = e.substr(2, 5)
      var mt = e.substr(9, 5)
      var nt = e.substr(16, 5)
      var pill = e.substr(25, 1)
      var capsule = e.substr(33, 1)
      var c = e.substr(35, 2)
      console.log(at, mt, nt, pill, capsule, c)
      this.setData({
        //修改标题
        top: '修改定时',
        MTime: '上午服药时间:' + mt,
        ATime: '下午服药时间:' + at,
        NTime: '晚上服药时间:' + nt,
        DrugsQuantityTitle: '药片:' + pill + '粒，胶囊:' + capsule + '粒',
        TimesOfTakingMedicine: "服药次数:" + c,
        bottom: '点击确认修改',

        //修改值
        mt: mt,
        at: at,
        nt: nt,
        capsuleValue: capsule,
        pillValue: pill,
        MedicineCountValue: c
      })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.userdata = wx.getStorageSync("userInfo")
    var that = this;
if (!that.userdata) {
  // 无数据

} else {
  // 有数据
  this.setData({
    isHide: false,
  });


 }  
},
  setCountValue1(e) {     //获取每次胶囊数量
    this.setData({ capsuleValue: e.detail.value })
  },
  setCountValue2(e) {     //获取共服用几次
    this.setData({ pillValue: e.detail.value })
  },
  setCountValue3(e) {     //获取共服用几次
    this.setData({ MedicineCountValue: e.detail.value })
  },
  //授权后跳入登录界面
  bindGetUserInfo: function (e) {
  
  },

  // setting: function () {   //跳转到设置界面
  //   wx.navigateTo({
  //     url: '/pages/setting/setting',
  //   })
  // },
  record: function () {    //跳转记录界面
    wx.navigateTo({
      url: '/pages/newRecord/newRecord',
    })
  },
  // shutdown: function () {  //关闭提醒
  //   var username = wx.getStorageSync('username');
  //   var that = this
  //   wx.request({
  //     url: "http://127.0.0.1:8080/MyServlet/findCount?username=" + username,
  //     method: 'GET',
  //     header: {
  //       'content-type': 'application/json',
  //     },
  //     success(res) {
  //       console.log("查询count请求成功", res)
  //       that.setData({
  //         count: res.data,
  //         modalName: 'show'
  //       })
  //     },
  //     fail(res) {
  //       console.log("查询count请求失败", res)
  //     }
  //   })
  // },

  // getOpenID: function () {
  //   wx.cloud.callFunction({
  //     name: 'getDate',
  //   }).then(res => {
  //     console.log('云函数获取到的openid:', res)
  //   })
  // },
  // hideModal() {
  //   this.setData({
  //     modalName: null
  //   })
  // },
  // off: function () {
  //   var that = this
  //   wx.request({
  //     url: "http://127.0.0.1:8080/MyServlet/timer?shutdown=1",
  //     method: 'GET',
  //     header: {
  //       'content-type': 'application/json',
  //     },
  //     success(res) {
  //       that.hideModal()
  //       wx.showToast({
  //         title: '关闭成功！', // 标题
  //         icon: 'success',  // 图标类型，默认success
  //         duration: 1500  // 提示窗停留时间，默认1500ms
  //       })
  //       console.log("关闭请求成功", res)
  //     },
  //     fail(res) {
  //       console.log("关闭请求失败", res)
  //     }
  //   })
  // },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})